package com.mercury.ec.module.air.dal.mysql.demandorder;

import com.mercury.ec.framework.common.pojo.PageResult;
import com.mercury.ec.framework.mybatis.core.mapper.BaseMapperX;
import com.mercury.ec.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.mercury.ec.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.mercury.ec.module.air.controller.admin.demandorder.vo.demandroute.AirDemandRoutePageReqVO;
import com.mercury.ec.module.air.controller.admin.demandorder.vo.demandroute.AirDemandRouteRespVO;
import com.mercury.ec.module.air.dal.dataobject.demandorder.AirDemandRouteDO;
import com.mercury.ec.module.basic.dal.dataobject.vendor.VendorDO;
import com.mercury.ec.module.system.dal.dataobject.dept.DeptDO;
import com.mercury.ec.module.system.dal.dataobject.user.AdminUserDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 机票需求订单方案行程 Mapper
 *
 * @author hesx
 */
@Mapper
public interface AirDemandRouteMapper extends BaseMapperX<AirDemandRouteDO> {

    default PageResult<AirDemandRouteDO> selectPage(AirDemandRoutePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<AirDemandRouteDO>()
                .eqIfPresent(AirDemandRouteDO::getDemandId, reqVO.getDemandId())
                .eqIfPresent(AirDemandRouteDO::getDemandSchemeId, reqVO.getDemandSchemeId())
                .eqIfPresent(AirDemandRouteDO::getSerialNo, reqVO.getSerialNo())
                .likeIfPresent(AirDemandRouteDO::getPnrContent, reqVO.getPnrContent())
                .eqIfPresent(AirDemandRouteDO::getPurchaseId, reqVO.getPurchaseId())
                .eqIfPresent(AirDemandRouteDO::getBookUserId, reqVO.getBookUserId())
                .eqIfPresent(AirDemandRouteDO::getBookDeptId, reqVO.getBookDeptId())
                .betweenIfPresent(AirDemandRouteDO::getBookTime, reqVO.getBookTime())
                .eqIfPresent(AirDemandRouteDO::getQuoteUserId, reqVO.getQuoteUserId())
                .eqIfPresent(AirDemandRouteDO::getQuoteDeptId, reqVO.getQuoteDeptId())
                .betweenIfPresent(AirDemandRouteDO::getQuoteTime, reqVO.getQuoteTime())
                .eqIfPresent(AirDemandRouteDO::getPricingUserId, reqVO.getPricingUserId())
                .eqIfPresent(AirDemandRouteDO::getPricingDeptId, reqVO.getPricingDeptId())
                .betweenIfPresent(AirDemandRouteDO::getPricingTime, reqVO.getPricingTime())
                .eqIfPresent(AirDemandRouteDO::getCancelRuleId, reqVO.getCancelRuleId())
                .eqIfPresent(AirDemandRouteDO::getCancelUserId, reqVO.getCancelUserId())
                .eqIfPresent(AirDemandRouteDO::getCancelDeptId, reqVO.getCancelDeptId())
                .betweenIfPresent(AirDemandRouteDO::getCancelTime, reqVO.getCancelTime())
                .eqIfPresent(AirDemandRouteDO::getCancelCause, reqVO.getCancelCause())
                .likeIfPresent(AirDemandRouteDO::getRemark, reqVO.getRemark())
                .eqIfPresent(AirDemandRouteDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(AirDemandRouteDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(AirDemandRouteDO::getId));
    }

    default List<AirDemandRouteRespVO> selectByDemandId(Long demandId) {
        return selectJoinList(AirDemandRouteRespVO.class, new MPJLambdaWrapperX<AirDemandRouteDO>()
                .eq(AirDemandRouteDO::getDemandId, demandId)
                .selectAll(AirDemandRouteDO.class)
                .selectAs(VendorDO::getName, AirDemandRouteRespVO::getPurchaseName)
                .selectAs(AdminUserDO::getNickname, AirDemandRouteRespVO::getBookUserNickname)
                .selectAs("u2.nickname", AirDemandRouteRespVO::getQuoteUserNickname)
                .selectAs("u3.nickname", AirDemandRouteRespVO::getPricingUserNickname)
                .selectAs("u4.nickname", AirDemandRouteRespVO::getCancelUserNickname)
                .selectAs(DeptDO::getName, AirDemandRouteRespVO::getBookDeptName)
                .selectAs("d2.name", AirDemandRouteRespVO::getQuoteDeptName)
                .selectAs("d3.name", AirDemandRouteRespVO::getPricingDeptName)
                .selectAs("d4.name", AirDemandRouteRespVO::getCancelDeptName)
                .leftJoin(VendorDO.class, VendorDO::getId, AirDemandRouteDO::getPurchaseId)
                .leftJoin(AdminUserDO.class, AdminUserDO::getId, AirDemandRouteDO::getBookUserId)
                .leftJoin(AdminUserDO.class, "u2", AdminUserDO::getId, AirDemandRouteDO::getQuoteUserId)
                .leftJoin(AdminUserDO.class, "u3", AdminUserDO::getId, AirDemandRouteDO::getPricingUserId)
                .leftJoin(AdminUserDO.class, "u4", AdminUserDO::getId, AirDemandRouteDO::getCancelUserId)
                .leftJoin(DeptDO.class, DeptDO::getId, AirDemandRouteDO::getBookDeptId)
                .leftJoin(DeptDO.class, "d2", DeptDO::getId, AirDemandRouteDO::getQuoteDeptId)
                .leftJoin(DeptDO.class, "d3", DeptDO::getId, AirDemandRouteDO::getPricingDeptId)
                .leftJoin(DeptDO.class, "d4", DeptDO::getId, AirDemandRouteDO::getCancelDeptId)
        );
    }

    default int deleteByDemandId(Long demandId) {
        return delete(AirDemandRouteDO::getDemandId, demandId);
    }
}
